package cn.fafu.fafu_record.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


/**
 * @description 文件上传工具
 * @date 2020年1月5日14:41:59
 */
public class FileUploadUtil {

    public static String uploadFile(MultipartFile file){

        if (file == null){
            return "";
        }
        String fileName = file.getOriginalFilename();
        System.out.println(fileName);
        System.out.println(fileName);

        if (fileName != null) {
            System.out.println("????");
            String newPath = fileName.substring(20,fileName.indexOf("."));
            String filePath = "/usr/local/java/images/"+newPath+"/";
            File path = new File(filePath);
            boolean mkdirs = false;
            if (!path.exists()){
                mkdirs = path.mkdirs();
            }
            else {
                mkdirs = true;
            }
            if (mkdirs){
                File dest = new File(filePath, fileName);
                try {
                    System.out.println("开始上传");
                    file.transferTo(dest);
                    return filePath + fileName;
                } catch (IOException e) {
                    System.out.println("图片上传失败！！！！"+e.getMessage());
                    return "";
                }
            }

        }
        System.out.println("返回null");
        return null;
    }
}
